package com.czh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czh.domain.ResponseResult;
import com.czh.domain.dto.QueryTagDto;
import com.czh.domain.vo.TagPageVo;
import com.czh.domain.vo.TagVo;
import com.czh.mapper.TagMapper;
import com.czh.domain.entity.Tag;
import com.czh.service.TagService;
import com.czh.utils.BeanCopyUtils;
import com.czh.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-03-28 09:31:05
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public ResponseResult<TagPageVo> pageTagList(Integer pageNum, Integer pageSize, QueryTagDto queryTagDto) {
        LambdaQueryWrapper<Tag> tagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tagLambdaQueryWrapper.like(StringUtils.hasText(queryTagDto.getName()), Tag::getName, queryTagDto.getName());
        tagLambdaQueryWrapper.like(StringUtils.hasText(queryTagDto.getRemark()), Tag::getRemark, queryTagDto.getRemark());

        Page<Tag> tagPage = page(new Page<>(pageNum, pageSize), tagLambdaQueryWrapper);
        TagPageVo pageVo = new TagPageVo(tagPage.getRecords(), tagPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult<Object> addTag(Tag tag) {
//        Long userId = SecurityUtils.getLoginUser().getUser().getId();
//        tag.setCreateBy(userId);
        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> deleteTag(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> updateTagById(Tag tag) {
        updateById(tag);
        return ResponseResult.okResult();
    }
    @Override
    public ResponseResult<TagVo> getTagById(Long id) {
        Tag tag = getById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult<List<Tag>> listAllTag() {
        return ResponseResult.okResult(list());
    }
}

